var Player1:PlayerObject;
var Player2:AIObject;
var Tower1:Tower;
var Tower2:Tower;
var TowerSpawner1:InstantiateBlocks;
var TowerSpawner2:InstantiateBlocks;
var atkHandlerPlayer1:MoveHandlerPlayer;
var atkHandlerPlayer2:MoveHandlerAI;
var buttonHandler1:ButtonHandler1;
var buttonHandler2:ButtonHandler2;
var ui:UIHandler;

var phaseDelay=10;
private var turnNum=1;

private var slotLoaded:boolean=false;


//Game Variables
private var player1winCheck:boolean=false;
private var player2winCheck:boolean=false;
private var player1forfeitCheck:boolean=false;
private var player2forfeitCheck:boolean=false;
private var randomGen:Random=new Random();



function LoadGame() {

	
		if(!slotLoaded)
		{
			Player1.ImportSlots();
			Player2.ImportSlots();
			slotLoaded=true;
			
			StartGame();
		}
	
}

function SetupGame(){
	ui.ShowMessage("Starting Setup...Please wait...",10);
	Player1.SetupPassive();
	//Debug.Log("Passive setup complete!");
	var player1SizeBonus= Player1.GetStartingSizeBonus();
	var player2SizeBonus= Player2.GetStartingSizeBonus();
	//Debug.Log("Passive Starting Size Found!");
	var player1SlotTypes= Player1.GetHandTypes();
	Debug.Log(player1SlotTypes);
	var player2SlotTypes= Player2.GetHandTypes();
	Debug.Log(player2SlotTypes);
	Debug.Log("Slot Types Found!");
	var i;
	for(i=0;i<5;i++)
	{
		var player1ST=player1SlotTypes[i];
		var player2ST=player2SlotTypes[i];
		TowerSpawner1.makeInitialBlocks(player1ST,player1SizeBonus,Tower1);
		TowerSpawner2.makeInitialBlocks(player2ST,player2SizeBonus,Tower2);
		yield StartCoroutine("InstantiateDelay");
	}
	Debug.Log("Initial Towers Built!");
	Debug.Log("Setup Done!");
	
}

function StartGame()
{
	yield StartCoroutine("SetupGame");
	var i=0;
	while(!(player1winCheck) && !(player2winCheck) && !(player1forfeitCheck) && !(player2forfeitCheck)){
		
		Debug.Log("Before Ego Reg");
		ui.ShowMessage("Turn " +turnNum + " starting...",5);
		yield StartCoroutine("Delay");
		ui.ShowMessage("Regenerating Ego...",5);
		yield StartCoroutine("EgoRegenPhase");
		Debug.Log("After Reg");
		yield StartCoroutine("Delay");
		ui.ShowMessage("Decaying Towers...",5);
		yield StartCoroutine ("TowerDecayPhase");
		Debug.Log("Waiting");
		yield StartCoroutine("Delay");
		Debug.Log("Waiting done...");
		ui.ShowMessage("Select your Spells!",10);
		yield StartCoroutine("SpellSelectionPhase");
		yield StartCoroutine("Delay");
		yield StartCoroutine("SpellResolutionPhase");
		yield StartCoroutine("Delay");
		yield StartCoroutine("VictoryCheck");
		turnNum++;
	}
	Debug.Log("Player 1 win!");
}
	
function EgoRegenPhase(){
		Debug.Log("Starting Ego Regen Phase...");
		Player1.EgoRegen();
		Player2.EgoRegen();
}

function TowerDecayPhase(){
	print("Starting Tower Decay Phase...");
	Tower1.DecayDeceptionBlocks();
	Tower2.DecayDeceptionBlocks();
}

function SpellSelectionPhase(){
	print("Starting Spell Selection Phase...");
	buttonHandler1.currentButtonSelected="";
	buttonHandler2.currentButtonSelected="";
	buttonHandler1.TurnButtonsOn();
	Player2.NextMove(Tower1,Player1,Tower2);
	print("Timer Start");
	ui.enableTimer();
	yield StartCoroutine("InputDelay");
	buttonHandler2.TurnButtonsOff();
	buttonHandler1.TurnButtonsOff();
	print("Timer End");
	
	
}

function SpellResolutionPhase(){
	print("Starting Spell Resolution Phase...");
	var player1Speed=GetSpeed(buttonHandler1.currentButtonSelected);
	var player2Speed=GetSpeed(buttonHandler2.currentButtonSelected);
	if (player1Speed>player2Speed){
		print("Executing Player 1's move...");
		atkHandlerPlayer1.ExecuteMove(buttonHandler1.currentButtonSelected);
		yield StartCoroutine("MoveDelay");
		print("Executing Player 2's move...");
		atkHandlerPlayer2.ExecuteMove(buttonHandler2.currentButtonSelected);
		yield StartCoroutine("MoveDelay");
	}
	else if (player2Speed>player1Speed){
		print("Executing Player 2's move...");
		atkHandlerPlayer2.ExecuteMove(buttonHandler2.currentButtonSelected);
		yield StartCoroutine("MoveDelay");
		print("Executing Player 2's move...");
		atkHandlerPlayer1.ExecuteMove(buttonHandler1.currentButtonSelected);
		yield StartCoroutine("MoveDelay");
	}
	else{
		var check=parseInt(randomGen.value*100);
		if ((check%2)==0){
			print("Executing Player 2's move...");
			atkHandlerPlayer2.ExecuteMove(buttonHandler2.currentButtonSelected);
			yield StartCoroutine("MoveDelay");
			print("Executing Player 2's move...");
			atkHandlerPlayer1.ExecuteMove(buttonHandler1.currentButtonSelected);
			yield StartCoroutine("MoveDelay");
		}
		else{
			print("Executing Player 1's move...");
			atkHandlerPlayer1.ExecuteMove(buttonHandler1.currentButtonSelected);
			yield StartCoroutine("MoveDelay");
			print("Executing Player 2's move...");
			atkHandlerPlayer2.ExecuteMove(buttonHandler2.currentButtonSelected);
			yield StartCoroutine("MoveDelay");
		}
	}
		

}

function VictoryCheck(){
	if(Tower1.height>=100){
		player1winCheck=true;
		ui.ShowMessage("Player 1 wins by Tower Height!",10);
		yield StartCoroutine("MoveDelay");
	}
	else if(Tower2.height>=100){
		player2winCheck=true;
		ui.ShowMessage("Player 2 wins by Tower Height!",10);
		yield StartCoroutine("MoveDelay");
	}
	else if(Tower1.height<=0){
		player2winCheck=true;
		ui.ShowMessage("Player 2's wins by destroying Player 1's Tower...",10);
		yield StartCoroutine("MoveDelay");
	}
	else if(Tower2.height<=0){
		player1winCheck=true;
		ui.ShowMessage("Player 1's wins by destroying Player 2's Tower...",10);
		yield StartCoroutine("MoveDelay");
		print("Player win");
		
	}
	else if(player1forfeitCheck){
		ui.ShowMessage("Player 1 forfeits....Player 2 wins!",10);
		yield StartCoroutine("MoveDelay");
	}
}



//Speed Finder
function GetSpeed(Type:String){
	if(Type=="11"){
		return parseInt((Player1.slots[0][8]));
	}
	else if(Type=="12"){
		return parseInt((Player1.slots[1][8]));
	}
	else if(Type=="13"){
		return parseInt((Player1.slots[2][8]));
	}
	else if(Type=="14"){
		return parseInt((Player1.slots[3][8]));
	}
	else if(Type=="15"){
		return parseInt((Player1.slots[4][8]));
	}
	else if(Type=="21"){
		return parseInt((Player2.slots[0][8]));
	}
	else if(Type=="22"){
		return parseInt((Player2.slots[1][8]));
	}
	else if(Type=="23"){
		return parseInt((Player2.slots[2][8]));
	}
	else if(Type=="24"){
		return parseInt((Player2.slots[3][8]));
	}
	else if(Type=="25"){
		return parseInt((Player2.slots[4][8]));
	}
	else if(Type=="Logic"){
		return 50;
	}
	else if(Type=="Deception"){
		return 60;
	}
	else if((Type=="Passion") || (Type=="Ethics")){
		return 10;
	}
	else if((Type=="")){
		return 0;
	}
}	

function Delay(){
	yield WaitForSeconds(phaseDelay);
}

function InputDelay(){
	yield WaitForSeconds(15);
}

function InstantiateDelay(){
	yield WaitForSeconds(2);
}

function MoveDelay(){
	yield WaitForSeconds(5);
}